package com.pearadmin.pro.modules.professor.service.impl;

import java.util.List;
import java.util.ArrayList;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.tools.core.GeneralConvertUtils;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;

import com.pearadmin.pro.modules.professor.domain.Professor;
import com.pearadmin.pro.modules.professor.domain.ProfessorEmploymentBackgroud;
import com.pearadmin.pro.modules.professor.dto.ProfessorDTO;
import com.pearadmin.pro.modules.professor.param.ProfessorRequest;
import com.pearadmin.pro.modules.professor.repository.ProfessorRepository;
import com.pearadmin.pro.modules.professor.service.ProfessorEmploymentBackgroudService;
import com.pearadmin.pro.modules.professor.service.ProfessorService;
import com.pearadmin.pro.modules.school.domain.CourseBasicInformation;
import com.pearadmin.pro.modules.school.domain.School;
import com.pearadmin.pro.modules.school.domain.SchoolSend;
import com.pearadmin.pro.modules.school.dto.SchoolDTO;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 教授信息Service业务层处理
 *
 * @author ChenJian
 * @date 2021-08-29
 */
@Service
public class ProfessorServiceImpl extends ServiceImpl<ProfessorRepository, Professor> implements ProfessorService
{
    @Resource
    private ProfessorRepository professorRepository;
    @Resource
    private ProfessorEmploymentBackgroudService professorEmploymentBackgroudService;

    /**
     * 查询教授信息
     * @param request
     * @return 教授信息 分页集合
     * */
    @Override
    public PageResponse<Professor> page(ProfessorRequest request)
    {
        LambdaQueryWrapper<Professor> query = new LambdaQueryWrapper<Professor>();
        query.eq(ObjectUtil.isNotEmpty(request.getFirstName()),Professor::getFirstName,request.getFirstName());
        query.eq(ObjectUtil.isNotEmpty(request.getPhoneNumbe()),Professor::getPhoneNumbe,request.getPhoneNumbe());
        return Pageable.of(request,(()->  professorRepository.selectList(query)));
    }

    @Override
    public Boolean saveOrUpdate(ProfessorDTO professor) {
        Professor professordb = GeneralConvertUtils.conv(professor, Professor.class);
        saveOrUpdate(professordb);
        if (!ObjectUtils.isEmpty(professor.getProfessorEmploymentBackgroud())) {
            List<ProfessorEmploymentBackgroud> employmentBackgroudList = professor.getProfessorEmploymentBackgroud();
            employmentBackgroudList.stream().forEach(s -> {
                s.setProfessorId(professordb.getId());
            });
            professorEmploymentBackgroudService.saveOrUpdateBatch(employmentBackgroudList);
        }
        return null;
    }

    /**
     * 查询教授信息
     *
     * @param id 教授信息ID
     * @return 教授信息
     */
    @Override
    public ProfessorDTO selectProfessorById(Long id)
    {
        ProfessorDTO request = GeneralConvertUtils.conv(professorRepository.selectById(id), ProfessorDTO.class);

        LambdaQueryWrapper<ProfessorEmploymentBackgroud> querySchoolSend = new LambdaQueryWrapper<>();
        querySchoolSend.eq(ProfessorEmploymentBackgroud::getProfessorId, id);
        request.setProfessorEmploymentBackgroud(professorEmploymentBackgroudService.list(querySchoolSend));

        return request;
    }

}
